package com.car.controller.car;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.entity.Car;
import com.car.service.car.CarService;
import com.car.util.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
//import sun.rmi.runtime.Log;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CarController {
    @Autowired
    private CarService carService;

    @RequestMapping("/toCarInfo")
    public ModelAndView toCarInfo(@RequestParam int carId) {
        ModelAndView mv = new ModelAndView("car-info");
        Car car = carService.getCarByID(carId);
        mv.addObject("car",car);
        return mv;
    }

    @RequestMapping("/getAllUsableCars")
    @ResponseBody
    public List<Car> getAllUsableCars(int departmentId) {
        List<Car> usableCars = carService.getAllUsableCars(departmentId);
        return usableCars;
    }

    @RequestMapping("/getCarByID")
    @ResponseBody
    public Car getCarByID(int carId) {
        return carService.getCarByID(carId);
    }

    @RequestMapping("/updateCarState")
    @ResponseBody
    public String updateCarState(Car car) {
        int result = carService.updateCarState(car.getCarId(), car.getCarState());
        return result == 1 ? "变更成功" : "变更失败";
    }

    //gh->
    @ResponseBody
    @RequestMapping("/toCarList")
    public ModelAndView toCarList(){
        return new ModelAndView("car-list");
    }

    @ResponseBody
    @RequestMapping("/carList")
    public Map CarList(String carIdCard,String PageNum, String PageSize){
        Integer pageNum = Integer.valueOf(PageNum);
        Integer pageSize = Integer.valueOf(PageSize);

        Map<String, Object> map = new HashMap<>();
        map.put("carIdCard", carIdCard);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);

        Page page = carService.selectLikeCar(map);
        map.put("page", page);
        map.put("totalPage", page.getTotal() / page.getSize() == 0 ?
                page.getTotal() / page.getSize() :
                (page.getTotal() / page.getSize() + 1)
        );
        return map;
    }

    @ResponseBody
    @RequestMapping("/toAddCar")
    public ModelAndView toAddInsurance(){
        return new ModelAndView("car-add");
    }

    @ResponseBody
    @RequestMapping("/addCar")
    public ResultMsg addCar(Car car,String userName,String departmentName){
        ResultMsg resultMsg = carService.addCar(car,userName,departmentName);
        return resultMsg;
    }

    @ResponseBody
    @RequestMapping("/delCar")
    public ResultMsg delCar(Car car) {
        ResultMsg resultMsg = carService.delCar(car);
        return resultMsg;
    }

    @ResponseBody
    @RequestMapping("/updateCar")
    public ResultMsg updateCar(Car car,String userName, String departmentName) {
        ResultMsg resultMsg = carService.updateCar(car,userName,departmentName);
        return resultMsg;
    }

    @ResponseBody
    @RequestMapping("/toUpdateCar")
    public ModelAndView toUpdateCar(Car car) {
        ModelAndView mv = new ModelAndView();
        ResultMsg resultMsg = carService.selectByCarId(car);
        mv.setViewName("car-edit");
        mv.addObject("result", resultMsg);
        return mv;
    }
    //<-gh
}
